layui.use(['form','table'],function () {
    var $ = layui.jquery,
        form = layui.form,
        table = layui.table;

    var dataTable = table.render({
        elem: '#menuTableId',
        url: '/menu/list',
        toolbar: '#toolbarDemo',
        cols: [[
            {field: 'id', title: '菜单ID', sort: true},
            {field: 'pid', title: '父菜单ID', sort: true},
            {field: 'title', title: '菜单名称', sort: true},
            {field: 'icon', title: '菜单图标'},
            {field: 'href', title: '请求路径', sort: true},
            {field: 'sort', title: '排序号', sort: true},
            {field: 'menuFlag', title: '菜单类型', sort: true, templet: function (d) {
                    if (d.menuFlag == 2) {
                        return '<span class="layui-badge layui-bg-gray">按钮</span>';
                    }
                    if (d.menuFlag == 0) {
                        return '<span class="layui-badge layui-bg-blue">目录</span>';
                    } else {
                        return '<span class="layui-badge-rim">菜单</span>';
                    }
                }
            },
            {field: 'createAt', title: '创建时间',templet:'<div>{{ layui.util.toDateString(d.createAt, "yyyy-MM-dd HH:mm:ss") }}</div>'},
            {field: 'updateAt',title: '更新时间',templet:'<div>{{ layui.util.toDateString(d.updateAt, "yyyy-MM-dd HH:mm:ss") }}</div>'},
            {title: '操作', minWidth: 150, toolbar: '#currentTableBar', align: "center"}
        ]],
        limits: [10, 15, 20, 25, 50, 100],
        limit: 10,
        page: true,
        skin: 'line'
    });


    /**
     * toolbar 头部监听事件
     */
    table.on('toolbar(currentTableFilter)', function (obj) {
        if (obj.event === 'delete') {  // 监听删除操作
            var checkStatus = table.checkStatus('menuTableId')
                , data = checkStatus.data;
            layer.alert(JSON.stringify(data));
        }
    });


    /**
     * 监听表格选择
     */
    table.on('tool(currentTableFilter)', function (obj) {
        var data = obj.data;
        if (obj.event === 'edit') {
            openWin('/page/editMenu.html?id=' + data.id,'编辑菜单',null);
        } else if (obj.event === 'copy') {  // 监听复制操作
            openWin("/page/editMenu.html?id="+ data.id,'复制菜单',null);
        }else if (obj.event === 'delete') {
            layer.confirm('确定删除菜单吗', function (index) {
                var modelId = data.id;
                $.ajax({
                    beforeSend: function() {
                        layer.load(2);
                    },
                    type: 'GET',
                    url: '/menu/delMenu',
                    data: {
                        id: id
                    },
                    dataType: 'json',
                    success: function (res) {
                        if(res.code == 200) {
                            layer.msg(res.msg, { icon: 1 ,time: 1000});
                            table.reload('menuTableId');
                        }else {
                            layer.msg(res.msg, {icon: 5, time: 2000});
                        }
                    },
                    complete: function() {
                        layer.closeAll("loading");
                    },
                    error: function() {
                        layer.msg('系统繁忙请稍后重试', {icon: 5, time: 2000});
                    }
                });
            });
        }
    });
});


function addModel() {
   openWin('/page/editMenu.html?id=','编辑菜单',null);
}


/**
 * 监听打开的弹窗，关闭后刷新页面
 */
function openWin(url,text,winInfo) {
    var winObj = window.open(url, text, winInfo);
    var loop = setInterval(function () {
        if (winObj.closed) {
            clearInterval(loop);
            //alert('closed');
            parent.location.reload();
        }
    }, 1);
}